package chances.declare.service;

import java.io.File;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Map;

import chances.declare.web.UnitContentCPContent;
import chances.system.operator.entity.EnumData;
import jxl.Workbook;
import jxl.write.Label;
import jxl.write.WritableCellFormat;
import jxl.write.WritableFont;
import jxl.write.WritableSheet;
import jxl.write.WritableWorkbook;
import jxl.write.WriteException;

public class WriteDeclareExcelService {
    public void exportExcel(String path, List<UnitContentCPContent> list1,
            List<EnumData> OrgEnumTypeResult, Map<String, String> contentTypeResult,
            Map<String, String> isAdoptedTypeResult, Map<String, String> exclusiveTypeResult) {
        WritableWorkbook book = null;
        WritableFont wf = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.NO_BOLD,
                false);
        WritableFont title = new WritableFont(WritableFont.createFont("宋体"), 10, WritableFont.BOLD,
                false);
        WritableCellFormat wcf = new WritableCellFormat(wf);
        WritableCellFormat wcf_title = new WritableCellFormat(title);
        SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        try {
            wcf.setWrap(true);// 单元格自动换行
            wcf.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
            wcf_title.setBorder(jxl.format.Border.ALL, jxl.format.BorderLineStyle.THIN);
        } catch (WriteException e1) {
            e1.printStackTrace();
        }
        System.out.println(path);
        String info1[] = { "CP名称", "片名", "类型", "归并名称", "归并状态", "是否独家", "采纳状态", "申报时间", "修改时间",
                "上线时间" };
        try {
            book = Workbook.createWorkbook(new File(path));
            // 生成工作表，参数0表示第一页
            WritableSheet sheet1 = book.createSheet("查询申报表", 0);
            sheet1.setColumnView(0, 30);// 设置列宽
            sheet1.setColumnView(1, 20);
            sheet1.setColumnView(2, 10);
            sheet1.setColumnView(3, 20);
            sheet1.setColumnView(4, 10);
            sheet1.setColumnView(5, 10);
            sheet1.setColumnView(6, 10);
            sheet1.setColumnView(7, 30);
            sheet1.setColumnView(8, 30);
            sheet1.setColumnView(9, 30);
            // 表头导航
            for (int j = 0; j < 10; j++) {
                Label label = new Label(j, 0, info1[j], wcf_title);
                sheet1.addCell(label);
            }
            String CPName = "";
            for (int i = 0; i < list1.size(); i++) {
                for (EnumData enumData : OrgEnumTypeResult) {
                    if (enumData.getValue()
                            .equals(list1.get(i).getCpContent().getOrgId().toString())) {
                        CPName = enumData.getName();
                    }
                }
                String declareTime = list1.get(i).getCpContent().getDeclareTime() == null ? ""
                        : sdf.format(list1.get(i).getCpContent().getDeclareTime());
                String updateTime = list1.get(i).getCpContent().getLastUpdateTime() == null ? ""
                        : sdf.format(list1.get(i).getCpContent().getLastUpdateTime());
                String onlineTime = list1.get(i).getCpContent().getPlanOnlineTime() == null ? ""
                        : sdf.format(list1.get(i).getCpContent().getPlanOnlineTime());
                sheet1.setRowView(i + 1, 350);// 设置行高
                sheet1.addCell(new Label(0, i + 1, CPName, wcf));
                sheet1.addCell(new Label(1, i + 1, list1.get(i).getCpContent().getName(), wcf));
                sheet1.addCell(new Label(2, i + 1,
                        contentTypeResult.get(list1.get(i).getCpContent().getContentType()), wcf));
                if (list1.get(i).getUnitContent() == null) {
                    sheet1.addCell(new Label(3, i + 1, "", wcf));
                } else {
                    sheet1.addCell(
                            new Label(3, i + 1, list1.get(i).getUnitContent().getName(), wcf));
                }
                sheet1.addCell(new Label(4, i + 1,
                        list1.get(i).getCpContent().getUnitContentId() == null ? "未归并" : "已归并",
                        wcf));
                sheet1.addCell(
                        new Label(5, i + 1,
                                exclusiveTypeResult
                                        .get(list1.get(i).getCpContent().getExclusive().toString()),
                        wcf));
                sheet1.addCell(
                        new Label(6, i + 1,
                                isAdoptedTypeResult
                                        .get(list1.get(i).getCpContent().getIsAdopted().toString()),
                        wcf));
                sheet1.addCell(new Label(7, i + 1, declareTime, wcf));
                sheet1.addCell(new Label(8, i + 1, updateTime, wcf));
                sheet1.addCell(new Label(9, i + 1, onlineTime, wcf));
            }
            // 写入数据并关闭文件
            book.write();
        } catch (Exception e) {
            System.out.println(e);
        } finally {
            if (book != null) {
                try {
                    book.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        }
    }
}
